package com.inossem.warehousestorage_inossem.adapter.refundStorage;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.refund_storage.RefundStorageMaterielDetailActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.RefundStorageOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.extra.RefundStorageExtra;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageOverviewBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

import static com.inossem.warehousestorage_inossem.R.id.serialNumber;

/**
 * Created by leij on 2017/6/6.
 */

public class RefundStorageItemListAdapter extends BaseAdapter {

    private Context mContext;
    private List<RefundStorageOverviewBean.DataBean.MaterielListBean> mRefundStorageItemList;
    private LayoutInflater inflater;
    private RefundStorageOverviewActivity.RefundStorageOverviewListener mRefundStorageOverviewListener;
    private String mMaterielCode, mPickingNumber, mPickingCompany, mCreateDate;

    public RefundStorageItemListAdapter(Context context, RefundStorageOverviewBean.DataBean data, List<RefundStorageOverviewBean.DataBean.MaterielListBean> refundStorageItemList, String materielCode, RefundStorageOverviewActivity.RefundStorageOverviewListener refundStorageOverviewListener) {
        mContext = context;
        mPickingNumber = data.getPickingNumber();
        mPickingCompany = data.getPickingCompany();
        mCreateDate = data.getCreateDate();
        mRefundStorageItemList = refundStorageItemList;
        mRefundStorageOverviewListener = refundStorageOverviewListener;
        inflater = LayoutInflater.from(context);
        mMaterielCode = materielCode;
    }

    @Override
    public int getCount() {
        return mRefundStorageItemList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mRefundStorageItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_refund_storage_item_list_first_item, null);
            TextView pickingNumber = (TextView) convertView.findViewById(R.id.pickingNumber);
            TextView pickingCompany = (TextView) convertView.findViewById(R.id.pickingCompany);
            TextView createDate = (TextView) convertView.findViewById(R.id.createDate);
            TextView showAndHide = (TextView) convertView.findViewById(R.id.showAndHide);
            PercentRelativeLayout hideLayout = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout);
            EditText materielCode = (EditText) convertView.findViewById(R.id.materielCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            pickingNumber.setText(mPickingNumber);
            pickingCompany.setText(mPickingCompany);
            createDate.setText(mCreateDate);
//            materielCode.setText(mMaterielCode);
            showAndHide.setVisibility(View.GONE);
            hideLayout.setVisibility(View.GONE);
//            showAndHide.setOnClickListener((View v) -> {
//                if (hideLayout.getVisibility() == View.GONE) {
//                    hideLayout.setVisibility(View.VISIBLE);
//                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_hide_information));
//                } else if (hideLayout.getVisibility() == View.VISIBLE) {
//                    hideLayout.setVisibility(View.GONE);
//                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_show_information));
//                }
//            });
            materielCode.addTextChangedListener(new QRCodeEditTextWatcher(materielCode) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, materielCode);
                    mRefundStorageOverviewListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mRefundStorageOverviewListener.onNoDoubleClickListener(materielCode.getText().toString());
                }
            });
        } else {
            RefundStorageOverviewBean.DataBean.MaterielListBean refundStorageItem = mRefundStorageItemList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_refund_storage_item_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, refundStorageItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.serialNumber = (TextView) view.findViewById(serialNumber);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescribe = (TextView) view.findViewById(R.id.materielDescribe);
        viewHolder.refundStorageNumber = (TextView) view.findViewById(R.id.refundStorageNumber);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, RefundStorageOverviewBean.DataBean.MaterielListBean refundStorageItem) {
        viewHolder.serialNumber.setText(refundStorageItem.getSerial());
        viewHolder.materielCode.setText(refundStorageItem.getMaterielCode());
        viewHolder.materielDescribe.setText(refundStorageItem.getMaterielDescription());
        viewHolder.refundStorageNumber.setText(TextUtils.isEmpty(refundStorageItem.getRefundNumber()) ? "输入数量" : ("退库数量" + refundStorageItem.getRefundNumber() + refundStorageItem.getUnit()));
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, RefundStorageMaterielDetailActivity.class);
                intent.putExtra(RefundStorageExtra.SERIAL, viewHolder.serialNumber.getText().toString());
                intent.putExtra(RefundStorageExtra.REFUND_STORAGE_PICKING_NUMBER, mPickingNumber);
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView serialNumber, materielCode, materielDescribe, refundStorageNumber;
    }
}
